package com.leesche.zcsp.node.web.controller.web.v2.param;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevConfigParam;
import com.leesche.zcsp.node.web.service.PubDevConfigParamService;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDevConfigParamCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * Created by dennies yang on 2017-01-05.
 */
@Controller
@RequestMapping("/v2/param")
public class WebParamController {
    private final static Logger LOGGER = LoggerFactory.getLogger(WebParamController.class);

    private static  final String PARAM_MAINVIEW="";

    @Autowired
    PrivilegeService privilegeService;

    @Autowired
    PubDevConfigParamService pubDevConfigParamService;


    /***
     * <p>参数组主界面显示</[p>
     * @param model
     * @param condition
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(Model model, QueryPubDevConfigParamCondition condition
    ) {

        return PARAM_MAINVIEW;
    }

    @ResponseBody
    @RequestMapping(value = "addParam", method = RequestMethod.POST)
    public Map<String,Object> addParam(PubDevConfigParam param){
        if(param.getParamgroupid()==null){
            return WebResult.failure("请选择参数组!").toMap();
        }
        if(StringUtils.isEmpty(param.getDevparamname())){
            return WebResult.failure("参数名称不能为空!").toMap();
        }
        param.setCreatetime(new Date());
        param.setUpdatetime(new Date());
        param.setAdminid(privilegeService.getCurrentPrincipal().getUserid());

        try{
            pubDevConfigParamService.insert(param);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamConfig isnert",e);
            return WebResult.failure(e).toMap();
        }
    }


    @ResponseBody
    @RequestMapping(value = "editParam", method = RequestMethod.POST)
    public Map<String,Object> editParam(PubDevConfigParam param){
        PubDevConfigParam pubDevConfigParam=pubDevConfigParamService.selectByPrimaryKey(param.getConfparamid());
        if(pubDevConfigParam.getAdminid()!=null&&!pubDevConfigParam.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数").toMap();
        }
        if(param.getParamgroupid()==null){
            return WebResult.failure("请选择参数组!").toMap();
        }
        if(StringUtils.isEmpty(param.getDevparamname())){
            return WebResult.failure("参数名称不能为空!").toMap();
        }
        param.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
        param.setCreatetime(new Date());
        param.setUpdatetime(new Date());

        try{
            pubDevConfigParamService.updateByPrimaryKey(param);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }

    @ResponseBody
    @RequestMapping(value = "deleteParamGroup", method = RequestMethod.POST)
    public Map<String,Object> deleteParamGroup(Long paramGroupId){
        PubDevConfigParam pubDevConfigParam=pubDevConfigParamService.selectByPrimaryKey(paramGroupId);
        if(pubDevConfigParam.getAdminid()!=null&&!pubDevConfigParam.getAdminid().equals(privilegeService.getCurrentPrincipal().getUserid())){
            return WebResult.failure("您无权限操作此参数").toMap();
        }
        try{
            pubDevConfigParamService.deleteByPrimaryKey(paramGroupId);
            return WebResult.success().info("操作成功!").toMap();
        }catch (Exception e){
            LOGGER.error("PubParamGroup isnert",e);
            return WebResult.failure(e).toMap();
        }
    }

}
